package com.haredot.vo;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 服务器接口返回对象
 * @author haredot
 * @version 1.0
 * @since 17
 * <br/>
 * getter方法公开，setter方法, 构造方法均进行私有
 * 无参构造, setter方法 主要是为了 能够进行 JSON反序列化 使用
 * 实现 Serializable 的目的是为了保证对象可以 进行 Java序列化
 * <br/>
 * 使用方式 ：
 * <br/>
 *   R<?> r = R.ok().build();  // 通用成功
 *   <br/>
 *   R<User>  r = R.<User>ok().data(user) ; // 成功并存储单个 user
 *   <br/>
 *   R<User>  r = R.<User>ok().data(List.of(user1, user2, ...)) ; // 成功并存储多个user
 *   <br/>
 *   R<?>  r = R.fail().build();  // 通用失败
 *   <br/>
 *   R<?>  r = R.fail().data(map); // 存储失败信息，失败信息放到 Map<String,String>中
 *   <br/>
 *   R<?>  r = R.fail(R.StatusCode.FORBIDDEN).data(map);  // 指定错误码和错误信息
 *   <br/>
 *   R<?>  r = R.fail(R.StatusCode.FORBIDDEN, "自定义错误消息").build();  // 自定义错误信息
 *   <br/>
 *   R<?>  r = R.fail(310000, "自定义错误消息").build();  // 自定义错误码和错误信息
 *   <br/>
 */
public final class R<T> implements Serializable {

    private static final long serialVersionUID = -673761974303645556L;
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 状态
     */
    private Boolean status;
    /**
     * 消息
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;
    /**
     * 数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;
    /**
     * 数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private List<T> list;
    /**
     * 错误信息
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Map<String, String> error;

    public Integer getCode() {
        return code;
    }

    private void setCode(Integer code) {
        this.code = code;
    }

    public Boolean getStatus() {
        return status;
    }

    private void setStatus(Boolean status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    private void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    private void setData(T data) {
        this.data = data;
    }

    public List<T> getList() {
        return list;
    }

    private void setList(List<T> list) {
        this.list = list;
    }

    public Map<String, String> getError() {
        return error;
    }

    private void setError(Map<String, String> error) {
        this.error = error;
    }

    private R() {
    }

    private R(Integer code, Boolean status, String message, T data) {
        this.code = code;
        this.status = status;
        this.message = message;
        this.data = data;
    }

    private R(String message, Integer code, Boolean status, List<T> list) {
        this.code = code;
        this.status = status;
        this.message = message;
        this.list = list;
    }

    private R(Boolean status, Integer code, String message, Map<String, String> error) {
        this.code = code;
        this.status = status;
        this.message = message;
        this.error = error;
    }

    /**
     * 返回成功建造者
     *
     * @param <E>
     * @return
     */
    public static <E> SuccessBuilder<E> ok() {
        return new SuccessBuilder<>();
    }

    /**
     * 返回通用失败建造者
     *
     * @param <E>
     * @return
     */
    public static <E> ErrorBuilder<E> fail() {
        return new ErrorBuilder<>(StatusCode.FAIL);
    }

    /**
     * 返回失败建造者，并指定失败的状态码
     *
     * @param <E>
     * @return
     */
    public static <E> ErrorBuilder<E> fail(StatusCode statusCode) {
        return new ErrorBuilder<>(statusCode);
    }

    public static <E> ErrorBuilder<E> fail(String message) {
        return fail(StatusCode.FAIL, message);
    }

    /**
     * 自定义错误信息
     *
     * @param statusCode
     * @param message
     * @param <E>
     * @return
     */
    public static <E> ErrorBuilder<E> fail(StatusCode statusCode, String message) {
        return new ErrorBuilder<>(statusCode.code, message);
    }

    public static <E> ErrorBuilder<E> fail(Integer code, String message) {
        return new ErrorBuilder<>(code, message);
    }

    /**
     * 成功建造者
     *
     * @param <E>
     */
    public static class SuccessBuilder<E> {
        /**
         * 设置列表数据
         *
         * @param list
         * @return
         */
        public R<E> data(List<E> list) {
            return new R<>(StatusCode.SUCCESS.message, StatusCode.SUCCESS.code, true, list);
        }

        /**
         * 设置单条数据
         *
         * @param data
         * @return
         */
        public R<E> data(E data) {
            return new R<>(StatusCode.SUCCESS.code, true, StatusCode.SUCCESS.message, data);
        }

        /**
         * 设置空数据
         *
         * @return
         */
        public R<E> build() {
            return new R<>(StatusCode.SUCCESS.code, true, StatusCode.SUCCESS.message, null);
        }
    }

    /**
     * 失败建造者
     *
     * @param <E>
     */
    public static class ErrorBuilder<E> {

        private Integer code;

        private String message;

        public ErrorBuilder(StatusCode statusCode) {
            this.code = statusCode.code;
            this.message = statusCode.message;
        }

        public ErrorBuilder(Integer code, String message) {
            this.code = code;
            this.message = message;
        }

        /**
         * 设置失败消息
         *
         * @param error
         * @return
         */
        public R<E> data(Map<String, String> error) {
            return new R<>(false, this.code, this.message, error);
        }

        /**
         * 设置空失败消息
         *
         * @return
         */
        public R<E> build() {
            return new R<>(false, this.code, this.message, null);
        }
    }

    /**
     * 定义 响应 状态码
     */
    public enum StatusCode {
        SUCCESS(10000, "操作成功"),
        FAIL(20000, "操作失败"),
        BAD(30000, "参数校验失败"),
        NOT_AUTH(40000, "认证失败"),
        ERROR(50000, "服务器异常"),
        FORBIDDEN(60000, "禁止访问"),
        NOT_ALLOW(70000, "请求方式不允许"),
        OTHER(99999, "未知错误");

        private final Integer code;

        private final String message;

        StatusCode(Integer code, String message) {
            this.code = code;
            this.message = message;
        }

    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", status=" + status +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", list=" + list +
                ", error=" + error +
                '}';
    }
}
